package com.tao.graduation.Controller;

import com.tao.graduation.Pojo.Appointment;
import com.tao.graduation.Pojo.Doctor;
import com.tao.graduation.Pojo.Hospitalization;
import com.tao.graduation.Pojo.Seek;
import com.tao.graduation.Service.AdminService;
import com.tao.graduation.Service.DoctorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/doctor")
public class DoctorController {

    @Autowired
    private DoctorService doctorService;

    @Autowired
    private AdminService adminService;

    private final  Integer LIMIT=5;

    @RequestMapping("/queryDoctorById")
    public Map queryDoctorById(@RequestBody Integer id) {
        Doctor doctor = doctorService.queryDoctorById(id);
        Map<String, Object> map = new HashMap<>();
        map.put("data", doctor);
        return map;
    }

    @RequestMapping("/queryCurrentAppointment")
    public Map queryCurrentAppointment( Integer doctorId,Integer page) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String today = simpleDateFormat.format(new Date());
        System.out.println(today);
        Integer index=LIMIT*(page-1);
        List<Map<Object, Object>> list = doctorService.queryCurrentAppointment(doctorId, today,index,LIMIT);
        Map<String, Object> map = new HashMap<>();
        System.out.println(list);
        map.put("data", list);
        return map;
    }

    @RequestMapping("/insertSeek")
    public Map insertSeek(String describes, String illName, Integer id, String certId) {
        int i = 0;
        Map<String, Object> map = new HashMap<>();
        Integer patientId = doctorService.queryPatientIdByCertId(certId);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String today = simpleDateFormat.format(new Date());
        Seek seek = new Seek(describes, illName, id, today, patientId);
        Integer count = doctorService.insertSeek(seek);
        if (count > 0) {
            map.put("message", "就诊完成!");
        } else {
            map.put("message", "信息录入失败!");
        }
        return map;
    }

    @RequestMapping("/Prescribe")
    public Map Prescribe(String drugs, String certId) {
        Map<String, Object> map = new HashMap<>();
        int i = 0;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String today = simpleDateFormat.format(new Date());
        Integer patientId = doctorService.queryPatientIdByCertId(certId);
        Integer count = doctorService.Prescribe(drugs, today, patientId);
        if (count > 0) {
            map.put("message", "记录成功!");
        } else {
            map.put("message", "记录失败!");
        }
        return map;
    }

    @RequestMapping("/insertHospitalization")
    public Map insertHospitalization(String floor, String door, String bed, String name, String medicalName
            , String inTime, String outTime) {
        int i = 0;
        Integer patientId = adminService.selectId(name);
        Hospitalization hospitalization = new Hospitalization(floor, door, bed, medicalName, patientId, inTime, outTime,"1");
        Map<String, Object> map = new HashMap<>();
        Integer count = doctorService.insertHospitalization(hospitalization);
        if (count > 0) {
            map.put("message", "申请成功!");
        } else {
            map.put("message", "申请失败!");
        }
        return map;
    }

    @RequestMapping("/SuccessSeek")
    public Map SuccessSeek(String certId,Integer doctorId){
        Map<String,Object> map=new HashMap<>();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String today = simpleDateFormat.format(new Date());
        Integer patientId = doctorService.queryPatientIdByCertId(certId);
        Integer count=doctorService.SuccessSeek(patientId,doctorId,"2",today);
        if (count>0){
            map.put("message","就诊完成");
        }else {
            map.put("message","信息有误!");
        }
        return map;
    }

    @RequestMapping("/querySeekJiLu")
    public Map querySeekJiLu(Integer doctorId,Integer page){
        Map<String,Object> map=new HashMap<>();
        Integer index=LIMIT*(page-1);
        List<Map<Object,Object>> list=doctorService.querySeekJiLu(doctorId,index,LIMIT);
        map.put("data",list);
        map.put("total",doctorService.querySeekJiLuCount(doctorId));
        return map;
    }

    @RequestMapping("/searchJiLu")
    public Map searchJiLu(String certId){
        Map<String,Object> map=new HashMap<>();
        Integer patientId=doctorService.queryPatientIdByCertId(certId);
        List<Map<Object,Object>> list=doctorService.searchJiLu(patientId);
        map.put("data",list);
        return map;
    }
}
